#!/usr/bin/python
# -*- coding: utf-8 -*-
import time
import smtplib

from email.header import Header
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
from email.mime.application import MIMEApplication

from config import settings


class MailHandler():
    def __init__(self):
        self.smtpObj = smtplib.SMTP()
        self.login()

    def login(self):
        try:
            self.smtpObj.connect(settings.MAIL_SERVER, settings.MAIL_PORT)
            self.smtpObj.login(settings.MAIL_SENDER, settings.MAIL_PWD)
        except smtplib.SMTPException as e:
            raise e
        return True

    def sendMail(self, MsgDec):
        self._send_(MsgDec)

    def _createMsg(self, MsgDec):
        message = MIMEMultipart()
        message["Subject"] = Header(MsgDec['Subject'], "utf-8")
        message['date'] = time.ctime()
        message["From"] = settings.MAIL_SENDER
        message["Cc"] = MsgDec['Cc']
        message["To"] = settings.MAIL_RECVER
        message.attach(MIMEText(MsgDec['Text'], "plain", "utf-8"))
        if MsgDec["File"] != None:
            print("Hello")
        return message

    def _send_(self, MsgDec):
        msg = self._createMsg(MsgDec)
        sender = settings.MAIL_SENDER
        receiver = settings.MAIL_RECVER
        if MsgDec['Cc'] != None:
            receiver += ';' + MsgDec['Cc']
        self.smtpObj.sendmail(sender, receiver, msg.as_string())

    def quit(self):
        self.smtpObj.quit()


class MailSSLHandler():
    def __init__(self):
        self.smtpSSLObj = smtplib.SMTP_SSL(settings.MAIL_SERVER, settings.MAIL_SSL_PORT)

    def ssl_login(self):
        try:
            self.smtpSSLObj.login(settings.MAIL_SENDER, settings.MAIL_PWD)
        except smtplib.SMTPException as e:
            raise e
        return True

    def sendMail(self, MsgDec):
        self._send_(MsgDec)

    def _createMsg(self, MsgDec):
        message = MIMEMultipart()
        message["Subject"] = Header(MsgDec['Subject'], "utf-8")
        message['date'] = time.ctime()
        message["From"] = MsgDec['From']
        message["Cc"] = MsgDec['Cc']
        message["To"] = MsgDec['To']
        message.attach(MIMEText(MsgDec['Text'], "plain", "utf-8"))
        if MsgDec["File"] != None:
            att = MIMEApplication(open(MsgDec['File'], 'rb').read())
            FileName = MsgDec['File'].split('\\')[-1]
            att.add_header('Content-Disposition', 'attachment', filename=FileName)
            message.attach(att)
        return message

    def _send_(self, MsgDec):
        msg = self._createMsg(MsgDec)
        sender = MsgDec['From']
        receiver = MsgDec['To']
        if MsgDec['Cc'] != None:
            receiver += (';' + MsgDec['Cc'])
        self.smtpSSLObj.sendmail(sender, receiver, msg.as_string())

    def quit(self):
        self.smtpSSLObj.close()


class ScriptMailHandler(MailHandler):
    def createMailMsg(self, Product):
        pass

    def review_mail_handler(self, json_value):
        pass

    def get_Case_Info(self):
        pass


if __name__ == '__main__':
    sslsmtp = MailSSLHandler()
    MsgDec = {
        "Subject": "test",
        "From": settings.MAIL_SENDER,
        "date": time.ctime(),
        "To": settings.MAIL_RECVER,
        "Cc": 'xxxxxxxxx@qq.com',
        "File": "./mail.py",
        "Text": "This is a TEST MAIL!",
    }
    sslsmtp.ssl_login()
    sslsmtp.sendMail(MsgDec)
    sslsmtp.quit()
